package model;

import bean.Poll;
import bean.PollUser;
import javax.mail.*;
import javax.mail.internet.*;
import java.security.Security;

import connection.DbConnection;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public abstract class EmailingSystem {
	
	/**
	 * Method used to send the username and password of newly created accounts to those users. See BRS page 7, 8 & 9.
	 * Used by Poll Administer, Poll Master and Poll Creator
	 * 
	 * Note: the "from" email address is incorrect.  Waiting for Jason's reply on correct system email address
	 * 
	 * @param pu
	 */
	public static void sendAccountEmail (PollUser pu) {
		
		try { 
			System.out.println("---------sendAccoutEmail method of EmailingSystem run");
			String recipients [] = {pu.getEmail()};
			String subject = "Account Details";
			String role;
			switch (pu.getAccessibility()) {
			case 1: role = "Poll User";	break;
			case 2: role = "Poll Master"; break;
			case 3: role = "Poll Creator"; break;
			case 4: role = "Poll Administrator"; break;
			case 5: role = "System Administrator"; break;
			default: throw new Exception();
			}
			String message = "Your "+role+" account has been created with the username = '"+pu.getUsername()+"' and password = '"+pu.getPassword()+"'.";
			String from = "s4099950@gmail.com"; // ********** Change me ***********
			
			GmailSystem.sendMessage(recipients, subject, message, from);
			
			//postMail (recipients, subject, message, from);
		} catch (Exception e) {
			System.out.println("**************** error in sendAccountEmail method of EmailingSystem");
			e.printStackTrace();
		}
		System.out.println("Email sent from sendAccountEmail method.");  // DELETE AFTER DEBUGGING
	}
	
	/**
	 * Method used to send the username and password of newly created accounts to those users. See BRS page 7, 8 & 9.
	 * Used by Poll Administer, Poll Master and Poll Creator
	 * 
	 * Note: the "from" email address is incorrect.  Waiting for Jason's reply on correct system email address
	 * 
	 * @param pu
	 */
	public static void sendAccountEmailGeneric (PollUser pu, String [] recipients) {
		
		try { 
			System.out.println("---------sendAccoutEmail method of EmailingSystem run");
			String subject = "Account Details";
			String role;
			switch (pu.getAccessibility()) {
			case 1: role = "Poll User";	break;
			case 2: role = "Poll Master"; break;
			case 3: role = "Poll Creator"; break;
			case 4: role = "Poll Administrator"; break;
			case 5: role = "System Administrator"; break;
			default: throw new Exception();
			}
			String message = "Your "+role+" account has been created with the username = '"+pu.getUsername()+"' and password = '"+pu.getPassword()+"'.";
			String from = "s4099950@gmail.com"; // ********** Change me ***********
			
			GmailSystem.sendMessage(recipients, subject, message, from);
			
			//postMail (recipients, subject, message, from);
		} catch (Exception e) {
			System.out.println("**************** error in sendAccountEmail method of EmailingSystem");
			e.printStackTrace();
		}
		System.out.println("Email sent from sendAccountEmail method.");  // DELETE AFTER DEBUGGING
	}
	
	/**
	 * Method used to inform Poll Master that a poll is ready to be conducted.  See BRS page 11.
	 * Method used by Poll Creator (through system)
	 * 
	 * Note: the "from" email address is incorrect.  Waiting for Jason's reply on correct system email address
	 * 
	 * @param p is Poll that is ready for conducting
	 */
	public static void sendInformativeEmail (Poll p) {
		
		try {
			// Build recipients string array
			ArrayList <String> array = new ArrayList <String>();
			ResultSet rs = null;
			DbConnection conn = new DbConnection();
			rs = conn.doSelect("SELECT * FROM User_Poll where POLLID = "+p.getId());
			while (rs.next()) {
				ResultSet rsTwo = null;
				rsTwo = conn.doSelect("SELECT * FROM PollUser where Username = '"+rs.getString("Username")+"' AND Accessibility = 2");
				rsTwo.next();
				array.add(rsTwo.getString("Email"));
			}
			//array.add("gayzy101@hotmail.com");
			System.out.println("array size is now "+array.size());
			
			String []recipients = new String[array.size()];
			for (int i = 0; i < array.size(); i++) {
				recipients[i] = array.get(i);
			}
			
			// Build subject, message and from for postMail method
			String subject = "Poll "+p.getName()+" assigned and ready";
			String message = "Poll "+p.getName()+" (Poll ID '"+p.getId()+"', Poll Version '"+p.getVersion()+"') has been assigned to you and is ready to be conducted.";
			String from = "s4099950@gmail.com"; // ********** Change me **********
			GmailSystem.sendMessage(recipients, subject, message, from);
			
		} catch (SQLException sql) {
			sql.printStackTrace();
			System.out.println("Problem with SQL query in sendInformativeEmail method of EmailingSystem class.");
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("Email sent from sendInformativeEmail method.");  // DELETE AFTER DEBUGGING
	}
	
	/** 
	 * Method used by sendAccountEmail and sendInformativeEmail methods.
	 * See 'http://www.javacommerce.com/displaypage.jsp?name=javamail.sql&id=18274' for help.
	 * 
	 * @param recipients is string array of recipients of email
	 * @param subject is email subject
	 * @param message is email content
	 * @param from is email address of system
	 * @throws MessagingException
	 */
	/**
	 * Method used to send the username and password of newly created accounts to those users. See BRS page 7, 8 & 9.
	 * Used by Poll Administer, Poll Master and Poll Creator
	 * 
	 * Note: the "from" email address is incorrect.  Waiting for Jason's reply on correct system email address
	 * 
	 * @param pu
	 */
	public static void sendPasswordChange (PollUser pu) {
		
		try { 
			System.out.println("---------sendPasswordChange method of EmailingSystem run");
			String recipients [] = {pu.getEmail()};
			String subject = "Password Changed";
			String role;
			switch (pu.getAccessibility()) {
			case 1: role = "Poll User";	break;
			case 2: role = "Poll Master"; break;
			case 3: role = "Poll Creator"; break;
			case 4: role = "Poll Administrator"; break;
			case 5: role = "System Administrator"; break;
			default: throw new Exception();
			}
			String message = "Your '"+pu.getUsername()+"' account password has changed to '"+pu.getPassword()+"'.";
			String from = "s4099950@gmail.com"; // ********** Change me ***********
			
			GmailSystem.sendMessage(recipients, subject, message, from);
			
			//postMail (recipients, subject, message, from);
		} catch (Exception e) {
			System.out.println("**************** error in sendAccountEmail method of EmailingSystem");
			e.printStackTrace();
		}
		System.out.println("Email sent from sendAccountEmail method.");  // DELETE AFTER DEBUGGING
	}
	
}
